package vn.com.vega.music.clientserver;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import vn.com.vega.music.objects.Video;
import vn.com.vega.music.utils.VegaLog;

public class JsonVideo extends JsonBase {

	public ArrayList<Video> videos;

	protected JsonVideo() {
	}

	public static JsonVideo loadVideoListByArtist(int artistId, int offset) {
		String url = ClientServer.getInstance().getUrlListVideoByArtist(
				artistId, offset);
		return loadVideoList(url);
	}

	public static JsonVideo search(String keyword, int offset) {
		String url = ClientServer.getInstance().getUrlSearchVideo(keyword,
				offset);
		return loadVideoList(url);
	}

	public static JsonVideo loadBillboardVideo() {
		String url = ClientServer.getInstance().getUrlBillboardVideo();
		return loadVideoList(url);
	}

	public static JsonVideo loadFavorite(int offset) {
		String url = ClientServer.getInstance().getUrlListFavoriteVideo(offset);
		return loadVideoList(url);
	}
	
	public static JsonVideo addFavorite(String videoId) {
		String url = ClientServer.getInstance().getUrlFavoriteVideo(videoId);
		JsonVideo jsonVideo = new JsonVideo();
		jsonVideo.requestAndParseBasicJson(url);
		return jsonVideo;
	}
	
	public static JsonVideo unFavorite(String videoId) {
		String url = ClientServer.getInstance().getUrlUnFavoriteVideo(videoId);
		JsonVideo jsonVideo = new JsonVideo();
		jsonVideo.requestAndParseBasicJson(url);
		return jsonVideo;
	}

	public static JsonVideo loadTopVideoList(int offset) {
		String url = ClientServer.getInstance().getUrlListTopVideo(offset);
		VegaLog.e(JsonVideo.class.getName(), "Load top video: " + url);
		return loadVideoList(url);
	}

	protected static JsonVideo loadVideoList(String url) {
		JsonVideo result = new JsonVideo();
		JSONObject json = result.requestAndParseBasicJson(url);
		if (result.isSuccess()) {
			result.parseVideo(json);
		}
		return result;
	}

	/**
	 * parse video json from server return: JSONVideo object
	 */
	protected void parseVideo(JSONObject json) {
		try {
			// channel category
			ArrayList<Video> detailVideos = new ArrayList<Video>();
			JSONArray aData = json.getJSONArray("data");
			for (int i = 0; i < aData.length(); i++) {
				JSONObject o = aData.getJSONObject(i);
				Video v = new Video();

				try {
					v.id = String.valueOf(o.getInt("id"));
					v.title = o.getString("title");
					v.thumbnail_large = o.getString("thumbnail_large");
					v.thumbnail_small = o.getString("thumbnail_large");
					v.duration = o.getInt("duration");
					// v.streamingUrl = o.getString("url_streaming");
					v.price = o.getString("price");
					v.artistName = o.getString("artist_name");
					v.viewCount = o.getInt("view_count");
					try {
						v.shareMessage = o.getString("share_message");
					} catch (Exception e) {
						// TODO: handle exception
					}
					try {
						v.listenPrice = o.getString("listten_price");
					} catch (Exception e) {
						// TODO: handle exception
					}
					try {
						v.downloadPrice = o.getString("download_price");
					} catch (Exception e) {
						// TODO: handle exception
					}
					try {
						String favorite = getString(o, "favorite", false);
						if (favorite.equals("")) {
							favorite = getString(o, "isFavorite", false);
						}
						v.isFavorite = favorite.equals("1");
					} catch (Exception e) {
						// TODO: handle exception
						v.isFavorite = false;
					}
				} catch (Exception e) {
					// TODO: handle exception
				}

				detailVideos.add(v);
			}
			videos = detailVideos;
		} catch (Exception ex) {
			setParsingError();
		}
	}
}
